package com.jade.yh.zsy.mobile.crm.api;

import com.jade.bss.base.RoleAction;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.web.resolver.CustomerCapsule;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.util.PagingResult;
import com.jade.framework.base.util.ResponseUtils;
import com.jade.zsy.base.common.ParametersException;
import com.jade.zsy.crm.entity.CrmInfo;
import com.jade.zsy.crm.entity.RechargeHis;
import com.jade.zsy.crm.service.CrmInfoFacade;
import com.jade.zsy.crm.service.IntegralHisFacade;
import com.jade.zsy.crm.service.RechargeHisFacade;
import com.jade.zsy.crm.vo.CrmInfoVo;
import com.jade.zsy.crm.vo.CrmIntegralVo;
import com.jade.zsy.crm.vo.IntegralHisVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/5
 * Time: 15:17
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/crm", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "会员管理", description = "会员模块--会员管理")
public class CrmApi {
    private CrmInfoFacade crmFacade = ApplicationContextUtils.getBean("CrmInfoFacade");
    private IntegralHisFacade integralHisFacade = ApplicationContextUtils.getBean("IntegralHisFacade");
    private RechargeHisFacade rechargeHisFacade = ApplicationContextUtils.getBean("RechargeHisFacade");
    @RequestMapping(value =  RoleAction.QUERY, method = RequestMethod.GET)
    @ApiOperation(value = "全部会员", httpMethod = "GET", response = CrmInfoVo.class)
    public void QUERY(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, CrmInfoVo entityVo) throws Exception {
        PagingResult result = crmFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    @RequestMapping(value =  "store", method = RequestMethod.GET)
    @ApiOperation(value = "店铺会员", httpMethod = "GET", response = CrmInfoVo.class)
    public void store(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, CrmInfoVo entityVo) throws Exception {
        PagingResult result = crmFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    @RequestMapping(value =  "my", method = RequestMethod.GET)
    @ApiOperation(value = "我的会员", httpMethod = "GET", response = CrmInfoVo.class)
    public void my(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, CrmInfoVo entityVo) throws Exception {
        PagingResult result = crmFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    //@RequiresAuthentication
    @ApiOperation(value = "会员详情")
    @RequestMapping(value =  RoleAction.GET, method = RequestMethod.GET)
    public void view(@ApiParam(value = "键值") @PathVariable(value = "id") long id,HttpServletResponse response)throws Exception{
        CrmInfo entity = crmFacade.getById(CrmInfo.class, id);
        if (null == entity) {
            throw new ParametersException("err.module.record.not_existed", "record not existed");
        }

        CrmInfoVo vo = CopyBean.copy(entity, CrmInfoVo.class);
        ResponseUtils.writeSuccessResult(response, vo);
    }

    @RequestMapping(value =  "integral", method = RequestMethod.GET)
    @ApiOperation(value = "会员积分历史", httpMethod = "GET", response = CrmIntegralVo.class)
    public void integral(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, IntegralHisVo entityVo) throws Exception {
        PagingResult result = integralHisFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    @RequestMapping(value =  "recharge", method = RequestMethod.GET)
    @ApiOperation(value = "会员充值历史", httpMethod = "GET", response = CrmIntegralVo.class)
    public void recharge(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                         @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                         @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                         @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                         @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                         HttpServletResponse response, IntegralHisVo entityVo) throws Exception {
        PagingResult result = rechargeHisFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    @RequestMapping(value =  "pay", method = RequestMethod.GET)
    @ApiOperation(value = "会员消费历史", httpMethod = "GET", response = CrmIntegralVo.class)
    public void pay(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                         @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                         @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                         @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                         @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                         HttpServletResponse response, IntegralHisVo entityVo) throws Exception {
        PagingResult result = rechargeHisFacade.queryEntity(entityVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }
}
